package javaCode.chapter13.list;

import java.util.Iterator;
import java.util.LinkedList;

/**
 * @author 徐州城
 * @version 1.0
 */
@SuppressWarnings({"all"})
public class LinkedListCRUD {
    public static void main(String[] args) {

        LinkedList linkedList = new LinkedList();
        linkedList.add(1);
        linkedList.add(2);
        linkedList.add(3);
        System.out.println("likedList=" + linkedList);

        //演示一个删除结点的
        linkedList.remove(); //这里默认删除第一个
        //linkedList.remove(2);
        System.out.println("likedList=" + linkedList);

        //修改某个结点对象
        linkedList.set(1,999);
        System.out.println("likedList=" + linkedList);

        //得到某个结点
        System.out.println(linkedList.get(1));

        //因为LinkedList 是 实现了List接口，遍历方式
        System.out.println("===LinkedList迭代器===");
        Iterator iterator = linkedList.iterator();
        while (iterator.hasNext()) {
            Object obj = iterator.next();
            System.out.println("obj=" + obj);
        }

        System.out.println("===增强for===");
        for (Object o :linkedList) {
            System.out.println("ob=" + o);
        }

        System.out.println("===for===");
        for (int i = 0; i < linkedList.size(); i++) {
            System.out.println("linkedList=" + linkedList.get(i));
        }

        //源码阅读
        /*1. LinkedList linkedList = new LinkedList();
             public LinkedList() {}
          2. 这时 linkedList 的属性 first和last 等于 null
          3. 执行
          public boolean add(E e) {
            linkLast(e);
            return true;
        }
          4. 将新的结点，加入到双向链表的最后
          void linkLast(E e) {
            final Node<E> l = last;
            final Node<E> newNode = new Node<>(l, e, null);
            last = newNode;
            if (l == null)
                first = newNode;
            else
                l.next = newNode;
            size++;
            modCount++;
        }
         */

        /*
        读源码 linkedList.remove();
        1. 执行
            public E remove() {
                return removeFirst();
            }
        2. 执行
            public E removeFirst() {
                final Node<E> f = first;
                if (f == null)
                    throw new NoSuchElementException();
                return unlinkFirst(f);
            }
         3. 执行 unlinkFirst(f); 将 f 指向的双向链表的第一个结点拿掉
             private E unlinkFirst(Node<E> f) {
                // assert f == first && f != null;
                final E element = f.item;
                final Node<E> next = f.next;
                f.item = null;
                f.next = null; // help GC
                first = next;
                if (next == null)
                    last = null;
                else
                    next.prev = null;
                size--;
                modCount++;
                return element;
            }

         */
    }
}
